package com.cooksys.film_rental.bo.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cooksys.film_rental.bo.AddressBo;
import com.cooksys.film_rental.bo.CityBo;
import com.cooksys.film_rental.bo.CountryBo;
import com.cooksys.film_rental.bo.CustomerBo;
import com.cooksys.film_rental.bo.ProfileFormBo;
import com.cooksys.film_rental.bo.StoreBo;
import com.cooksys.film_rental.model.Address;
import com.cooksys.film_rental.model.City;
import com.cooksys.film_rental.model.Country;
import com.cooksys.film_rental.model.Customer;
import com.cooksys.film_rental.model.Store;
import com.cooksys.film_rental.model.forms.ProfileForm;

@Service("ProfileFormBo")
public class ProfileFormBoImpl implements ProfileFormBo {

	@Autowired
	CountryBo countryBo;

	@Autowired
	CityBo cityBo;

	@Autowired
	StoreBo storeBo;

	@Autowired
	AddressBo addressBo;

	@Autowired
	CustomerBo customerBo;

	@Override
	public ProfileForm populateProfileFormByCustomer(Customer customer) {
		ProfileForm result = new ProfileForm();

		if (customer != null) {

			result.setCustomerId(customer.getCustomerId());
			result.setFirstName(customer.getFirstName());
			result.setLastName(customer.getLastName());
			result.setEmail(customer.getEmail());

			Address address = addressBo.getAddressByCustomer(customer);

			if (address != null) {

				result.setAddressLine1(address.getAddress());
				result.setAddressLine2(address.getAddress2());
				result.setDistrict(address.getDistrict());
				result.setPhoneNumber(address.getPhone());
				result.setPostalCode(address.getPostalCode());

				City city = cityBo.getCityByAddress(address);

				if (city != null) {

					result.setCityName(city.getCity());

					Country country = countryBo.getCountryByCity(city);

					if (country != null) {

						result.setCountryId(country.getCountryId().toString());

					}

				}
			}

			Store store = storeBo.getStoreByCustomer(customer);

			if (store != null) {

				result.setStoreId(store.getStoreId().toString());

			}
		}

		return result;
	}

	@Override
	public Customer updateCustomerFromProfileForm(ProfileForm profileForm) {
		Customer customer = null;

		if (profileForm.getCustomerId() != null
				&& (customer = customerBo.getCustomerByCustomerId(profileForm
						.getCustomerId())) != null) {

			Country customerCountry = countryBo
					.getCountryByCountryId(profileForm.getCountryId());

			City formCity = new City();
			formCity.setCity(profileForm.getCityName());
			formCity.setCountry(customerCountry);
			City customerCity = cityBo.matchCityToDatabase(formCity,
					customerCountry);

			if (customerCity == null) {
				customerCity = cityBo.save(formCity);
			}

			Address formAddress = new Address();

			formAddress.setAddress(profileForm.getAddressLine1());
			formAddress.setAddress2(profileForm.getAddressLine2());
			formAddress.setDistrict(profileForm.getDistrict());
			formAddress.setPhone(profileForm.getPhoneNumber());
			formAddress.setPostalCode(profileForm.getPostalCode());
			formAddress.setCity(customerCity);

			Address customerAddress = addressBo.matchAddressToDatabase(
					formAddress, customerCity);

			if (customerAddress == null) {
				customerAddress = addressBo.save(formAddress);
			}

			Store customerStore = storeBo.getStoreByStoreId(profileForm
					.getStoreId());

			customer.setFirstName(profileForm.getFirstName());
			customer.setLastName(profileForm.getLastName());
			customer.setEmail(profileForm.getEmail());
			customer.setAddress(customerAddress);
			customer.setStore(customerStore);

			customerBo.update(customer);
		}

		return customer;
	}
}
